package com.cjkj.applet.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cjkj.applet.domain.dto.EcgAnalysisParam;
import com.cjkj.applet.domain.vo.EcgAnalysisData;
import com.cjkj.applet.service.TJ03ReportService;
import com.cjkj.common.core.utils.StringUtils;
import com.cjkj.common.core.utils.http.HttpUtils;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.system.api.RemoteTJ03HealthReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TJ03ReportServiceImpl implements TJ03ReportService {

    @Resource
    private RemoteTJ03HealthReportService remoteTJ03HealthReportService;

    @Override
    public AjaxResult getTj03HeartReport(Long visitNo) {
        return remoteTJ03HealthReportService.getHeartReports(visitNo);
    }

    @Override
    public AjaxResult getTj03EcgReport(Long visitNo) {
        return remoteTJ03HealthReportService.getEcgReports(visitNo);
    }

    @Override
    public AjaxResult getTj03BPReport(Long visitNo) {
        return remoteTJ03HealthReportService.getBPReports(visitNo);
    }

    @Override
    public EcgAnalysisData getEcgAnalysisData(EcgAnalysisParam param) {
        String ecgs = param.getEcgs();
        String[] split = ecgs.split(",");
        List<Integer> ecgList = new ArrayList<>();
        for (String ecg : split) {
            ecgList.add((int) (Double.parseDouble(ecg) * 1000));
        }
        String ecgArrStr = ecgList.stream().map(String::valueOf).collect(Collectors.joining(","));
        String resp = HttpUtils.sendPost("http://121.36.108.144:9527/cyzur/api/ecgData", "ecgs=" + ecgArrStr);
        if(StringUtils.isNotBlank(resp)) {
            JSONObject jsonObject = JSON.parseObject(resp);
            Integer code = jsonObject.getInteger("code");
            if(0 == code) {
                JSONObject result = jsonObject.getJSONObject("data");
                if(result != null) {
                    return result.toJavaObject(EcgAnalysisData.class);
                }
            }
        }
        return new EcgAnalysisData();
    }
}
